<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<link rel="STYLESHEET" href="lib.css" type='text/css' />
<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
<link rel='start' href='../index.html' title='Python documentation Index' />
<link rel="first" href="lib.html" title='Python library Reference' />
<link rel='contents' href='contents.html' title="Contents" />
<link rel='index' href='genindex.html' title='Index' />
<link rel='last' href='about.html' title='About this document...' />
<link rel='help' href='about.html' title='About this document...' />
<link rel="next" href="rexec-extension.html" />
<link rel="prev" href="module-rexec.html" />
<link rel="parent" href="module-rexec.html" />
<link rel="next" href="rexec-extension.html" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name='aesop' content='information' />
<title>28.1.1 RExec Objects </title>
</head>
<body>
<div class="navigation">
<div id='top-navigation-panel' xml:id='top-navigation-panel'>
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="28.1 rexec  "
  href="module-rexec.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="28.1 rexec  "
  href="module-rexec.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="28.1.2 defining restricted environments"
  href="rexec-extension.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-rexec.html">28.1 rexec  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-rexec.html">28.1 rexec  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="rexec-extension.html">28.1.2 Defining restricted environments</a>
</div>
<hr /></div>
</div>
<!--End of Navigation Panel-->

<h2><a name="SECTION0030110000000000000000"></a><a name="rexec-objects"></a>
<br>
28.1.1 RExec Objects 
</h2>

<p>
<tt class="class">RExec</tt> instances support the following methods:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5332' xml:id='l2h-5332' class="method">r_eval</tt></b>(</nobr></td>
  <td><var>code</var>)</td></tr></table></dt>
<dd>
<var>code</var> must either be a string containing a Python expression, or
a compiled code object, which will be evaluated in the restricted
environment's <tt class="module">__main__</tt> module.  The value of the expression or
code object will be returned.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5333' xml:id='l2h-5333' class="method">r_exec</tt></b>(</nobr></td>
  <td><var>code</var>)</td></tr></table></dt>
<dd>
<var>code</var> must either be a string containing one or more lines of
Python code, or a compiled code object, which will be executed in the
restricted environment's <tt class="module">__main__</tt> module.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5334' xml:id='l2h-5334' class="method">r_execfile</tt></b>(</nobr></td>
  <td><var>filename</var>)</td></tr></table></dt>
<dd>
Execute the Python code contained in the file <var>filename</var> in the
restricted environment's <tt class="module">__main__</tt> module.
</dl>

<p>
Methods whose names begin with "<tt class="samp">s_</tt>" are similar to the functions
beginning with "<tt class="samp">r_</tt>", but the code will be granted access to
restricted versions of the standard I/O streams <code>sys.stdin</code>,
<code>sys.stderr</code>, and <code>sys.stdout</code>.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5335' xml:id='l2h-5335' class="method">s_eval</tt></b>(</nobr></td>
  <td><var>code</var>)</td></tr></table></dt>
<dd>
<var>code</var> must be a string containing a Python expression, which will
be evaluated in the restricted environment.  
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5336' xml:id='l2h-5336' class="method">s_exec</tt></b>(</nobr></td>
  <td><var>code</var>)</td></tr></table></dt>
<dd>
<var>code</var> must be a string containing one or more lines of Python code,
which will be executed in the restricted environment.  
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5337' xml:id='l2h-5337' class="method">s_execfile</tt></b>(</nobr></td>
  <td><var>code</var>)</td></tr></table></dt>
<dd>
Execute the Python code contained in the file <var>filename</var> in the
restricted environment.
</dl>

<p>
<tt class="class">RExec</tt> objects must also support various methods which will be
implicitly called by code executing in the restricted environment.
Overriding these methods in a subclass is used to change the policies
enforced by a restricted environment.

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5338' xml:id='l2h-5338' class="method">r_import</tt></b>(</nobr></td>
  <td><var>modulename</var><big>[</big><var>, globals</var><big>[</big><var>,
                             locals</var><big>[</big><var>, fromlist</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Import the module <var>modulename</var>, raising an <tt class="exception">ImportError</tt>
exception if the module is considered unsafe.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5339' xml:id='l2h-5339' class="method">r_open</tt></b>(</nobr></td>
  <td><var>filename</var><big>[</big><var>, mode</var><big>[</big><var>, bufsize</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Method called when <tt class="function">open()</tt> is called in the restricted
environment.  The arguments are identical to those of <tt class="function">open()</tt>,
and a file object (or a class instance compatible with file objects)
should be returned.  <tt class="class">RExec</tt>'s default behaviour is allow opening
any file for reading, but forbidding any attempt to write a file.  See
the example below for an implementation of a less restrictive
<tt class="method">r_open()</tt>.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5340' xml:id='l2h-5340' class="method">r_reload</tt></b>(</nobr></td>
  <td><var>module</var>)</td></tr></table></dt>
<dd>
Reload the module object <var>module</var>, re-parsing and re-initializing it.  
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5341' xml:id='l2h-5341' class="method">r_unload</tt></b>(</nobr></td>
  <td><var>module</var>)</td></tr></table></dt>
<dd>
Unload the module object <var>module</var> (remove it from the
restricted environment's <code>sys.modules</code> dictionary).
</dl>

<p>
And their equivalents with access to restricted standard I/O streams:

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5342' xml:id='l2h-5342' class="method">s_import</tt></b>(</nobr></td>
  <td><var>modulename</var><big>[</big><var>, globals</var><big>[</big><var>,
                             locals</var><big>[</big><var>, fromlist</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
<dd>
Import the module <var>modulename</var>, raising an <tt class="exception">ImportError</tt>
exception if the module is considered unsafe.
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5343' xml:id='l2h-5343' class="method">s_reload</tt></b>(</nobr></td>
  <td><var>module</var>)</td></tr></table></dt>
<dd>
Reload the module object <var>module</var>, re-parsing and re-initializing it.  
</dl>

<p>
<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
  <td><nobr><b><tt id='l2h-5344' xml:id='l2h-5344' class="method">s_unload</tt></b>(</nobr></td>
  <td><var>module</var>)</td></tr></table></dt>
<dd>
Unload the module object <var>module</var>.   
</dl>

<p>

<div class="navigation">
<div class='online-navigation'>
<p></p><hr />
<table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td class='online-navigation'><a rel="prev" title="28.1 rexec  "
  href="module-rexec.html"><img src='../icons/previous.png'
  border='0' height='32'  alt='Previous Page' width='32' /></a></td>
<td class='online-navigation'><a rel="parent" title="28.1 rexec  "
  href="module-rexec.html"><img src='../icons/up.png'
  border='0' height='32'  alt='Up one Level' width='32' /></a></td>
<td class='online-navigation'><a rel="next" title="28.1.2 defining restricted environments"
  href="rexec-extension.html"><img src='../icons/next.png'
  border='0' height='32'  alt='Next Page' width='32' /></a></td>
<td align="center" width="100%">Python Library Reference</td>
<td class='online-navigation'><a rel="contents" title="Table of Contents"
  href="contents.html"><img src='../icons/contents.png'
  border='0' height='32'  alt='Contents' width='32' /></a></td>
<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
  border='0' height='32'  alt='Module Index' width='32' /></a></td>
<td class='online-navigation'><a rel="index" title="Index"
  href="genindex.html"><img src='../icons/index.png'
  border='0' height='32'  alt='Index' width='32' /></a></td>
</tr></table>
<div class='online-navigation'>
<b class="navlabel">Previous:</b>
<a class="sectref" rel="prev" href="module-rexec.html">28.1 rexec  </a>
<b class="navlabel">Up:</b>
<a class="sectref" rel="parent" href="module-rexec.html">28.1 rexec  </a>
<b class="navlabel">Next:</b>
<a class="sectref" rel="next" href="rexec-extension.html">28.1.2 Defining restricted environments</a>
</div>
</div>
<hr />
<span class="release-info">Release 2.5.1, documentation updated on 18th April, 2007.</span>
</div>
<!--End of Navigation Panel-->
<address>
See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
</address>
</body>
</html>
